﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlDePeso.EncryptControlDePesoWebReference;

namespace ControlDePeso.Form
{
    public partial class frmInicio : frmControlDePeso
    {
        EncryptControlDePesoWebReference.EncryptControlDePesoSoapClient _encryptControlDePeso = new EncryptControlDePesoSoapClient();

        public frmInicio()
        {
            InitializeComponent();
        }

        #region Public Methods

        /// <summary>
        /// Handles the <see cref="btn_Ok_Click"/> event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void btnOK_Click(object sender, EventArgs e)
        {
            int rows = 0;
            DataBase DB = new DataBase();
            StringBuilder command = new StringBuilder();

            if (this.chkNewUser.Checked)
            {
                frmConfirmPassword confirmPassword = new frmConfirmPassword(this.txtUser.Text);
                confirmPassword.ShowDialog(this);

                if (_encryptControlDePeso.Encrypt(this.txtPassword.Text) != confirmPassword._encryptedPassword)
                {
                    MessageBox.Show("La contraseña debe de ser la misma, vuelva a interntarlo", "Control de Peso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    command = new StringBuilder("INSERT INTO UserLogin ([userName], [password]) ");
                    command.Append("values ('" + _encryptControlDePeso.Encrypt(this.txtUser.Text) + "', '" + _encryptControlDePeso.Encrypt(this.txtPassword.Text) + "')");
                }
            }
            else
            {
                command = new StringBuilder("SELECT [UserName] FROM UserLogin WHERE [UserName] = '" + _encryptControlDePeso.Encrypt(this.txtUser.Text) + "' AND [Password]  = '" + _encryptControlDePeso.Encrypt(this.txtPassword.Text) + "'");
            }

            if (command != null && command.Length != 0)
            {
                try
                {
                    this.UserName =  (string)DB.ExecuteScalar(command.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error: Control de peso");
                }
            }

            if (rows == 1 || this.UserName != null)
            {
                this.Hide();
                frmChart chart = new frmChart(this.txtUser.Text);
                chart.ShowDialog();
            }
            else if(command.Length != 0)
            {
                MessageBox.Show(this, "Usuario o contraseña equivodada, favor de intentar de nuevo", "Control de Peso", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        #endregion Public Methods

        
    }
}
